package com.flipdf2.serialize.ppml.element;

import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.List;

import org.jdom.Element;
import org.jdom.Namespace;

import com.flipdf2.serialize.ppml.PPMLException;

public class PPMLSource {
	public static final String NAME = "SOURCE";
	
	private List<PPMLInternalData> internalDataList;
	private List<PPMLExternalData> externalDataList;
	
	private PPMLExternalDataArray externalDataArray;
	
	private String format;
	private double[] dimensions;
	private Rectangle clippingBox;
	
	public Element toXML(Namespace defaultNamespace) {
		Element root = null;
		if (defaultNamespace != null) {
			root = new Element(NAME, defaultNamespace);
		} else {
			root = new Element(NAME);	
		}
		
		if (internalDataList != null && internalDataList.size() > 0) {
			for (int i = 0, n = internalDataList.size(); i < n; i++) {
				root.addContent(internalDataList.get(i).toXML(defaultNamespace));
			}
		}
		
		if (externalDataList != null && externalDataList.size() > 0) {
			for (int i = 0, n = externalDataList.size(); i < n; i++) {
				root.addContent(externalDataList.get(i).toXML(defaultNamespace));
			}
		}
		
		if (externalDataArray != null)
			root.addContent(externalDataArray.toXML(defaultNamespace));
		
		if (format != null) 
			root.setAttribute("Format", format);
		if (dimensions != null)
			root.setAttribute("Dimensions", dimensions[0] + " " + dimensions[1]);
		if (clippingBox != null) {
			root.setAttribute("ClippingBox", clippingBox.x + " " + clippingBox.y
				+ " " + clippingBox.width + " " + clippingBox.height);
		}
		
		return root;
	}

	public void check() throws PPMLException {
		if (format == null) {
			throw PPMLException.getInstance(PPMLException.REQUIRED_ATTRIBUTE_LOST, "Format");
		}
		if (dimensions == null) {
			throw PPMLException.getInstance(PPMLException.REQUIRED_ATTRIBUTE_LOST, "Dimensions");
		}
		if (internalDataList == null && externalDataList == null && externalDataArray == null) {
			throw PPMLException.getInstance(PPMLException.REQUIRED_ELEMENT_LOST
					, "((INTERNAL_DATA | EXTERNAL_DATA)+ | EXTERNAL_DATA_ARRAY)");
		}
	}
	
	public Rectangle getClippingBox() {
		return clippingBox;
	}

	public void setClippingBox(Rectangle clippingBox) {
		this.clippingBox = clippingBox;
	}

	public double[] getDimensions() {
		return dimensions;
	}

	public void setDimensions(double[] dimensions) {
		this.dimensions = dimensions;
	}

	public PPMLExternalDataArray getExternalDataArray() {
		return externalDataArray;
	}

	public void setExternalDataArray(PPMLExternalDataArray externalDataArray) {
		this.externalDataArray = externalDataArray;
	}
	
	public List<PPMLExternalData> getExternalDataList() {
		return externalDataList;
	}

	public void setExternalDataList(List<PPMLExternalData> externalDataList) {
		this.externalDataList = externalDataList;
	}
	
	public void addExternalData(PPMLExternalData externalData) {
		if (this.externalDataList == null)
			this.externalDataList = new ArrayList<PPMLExternalData>();
		this.externalDataList.add(externalData);
	}
	
	public String getFormat() {
		return format;
	}

	public void setFormat(String format) {
		this.format = format;
	}

	public List<PPMLInternalData> getInternalDataList() {
		return internalDataList;
	}

	public void setInternalDataList(List<PPMLInternalData> internalDataList) {
		this.internalDataList = internalDataList;
	}
	
	public void addInternalData(PPMLInternalData internalData) {
		if (this.internalDataList == null)
			this.internalDataList = new ArrayList<PPMLInternalData>();
		this.internalDataList.add(internalData);
	}
}
